<template>
    <div>
        <s-search-bar>
            <el-form :model="rfidEquipment" label-width="110px">
                <el-form-item label="小区" prop="orgId" v-hasOrg>
                    <org-select v-model="rfidEquipment.orgId"  @select="query" :show-all="true"></org-select>
                </el-form-item>
                <el-form-item prop="rfidEquipmentName" label="RFID设备名称">
                    <el-input v-model="rfidEquipment.rfidEquipmentName"></el-input>
                </el-form-item>
                <el-form-item prop="rfidEquipmentCode" label="RFID设备编码">
                    <el-input v-model="rfidEquipment.rfidEquipmentCode"></el-input>
                </el-form-item>
                <el-button @click="addRfidEquipment" type="success" style="margin-left:50px">新增</el-button>
                    <el-button @click="query" icon="el-icon-search" type="primary">查询</el-button>
            </el-form>
        </s-search-bar>

        <basic-table :tableLoading="tableLoading" :tableData="tableData" :tableOption="tableOption" :tablePage="tablePage" @table-search="query">
            <template slot="scope" slot-scope="scope">
                <el-tag type="success">{{scope.scope.row.isAlarmName}}</el-tag>
                <!-- <el-button type="text" icon="el-icon-view" @click="seePhoto(scope.scope.row,scope.scope.$index)">查看</el-button> -->
            </template>
          <template slot="isConnect" slot-scope="scope">
            <el-tag type="success"> {{scope.scope.row.isConnect ==='1'?'未连接':'连接'}}</el-tag>
          </template>
          <template slot="isInv" slot-scope="scope">
            <el-tag type="success"> {{scope.scope.row.isInv ==='0'?'已盘点':'停止'}}</el-tag>
          </template>
            <template slot="operation" slot-scope="scope">
                <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.scope.row,scope.scope.$index)">修改</el-button>
                <el-button type="text" icon="el-icon-delete" @click="rowDel(scope.scope.row,scope.scope.$index)">删除</el-button>
                <el-button type="text" icon="el-icon-edit" @click="connect(scope.scope.row,scope.scope.$index)">
                  {{scope.scope.row.isConnect ==='1'?'连接':'断开连接'}}</el-button>
                <el-button type="text" icon="el-icon-edit" @click="setPower(scope.scope.row)">设置功率</el-button>
                <el-button type="text" icon="el-icon-edit" @click="invData(scope.scope.row,scope.scope.$index)">
                  {{scope.scope.row.isInv ==='1'?'盘点':'停止'}}</el-button>
            </template>
        </basic-table>
        <add-rfid-equipment ref="add-rfid-equipment" @table-search="query"></add-rfid-equipment>
        <photo-view ref="photo-view"></photo-view>

        <el-dialog title="设置功率" :visible="rfidShowDialog" :before-close="handleClose" width="500px">

            <el-form ref="rfidInfo" :model="rfidEquipment" label-width="100px">
                <el-form-item prop="rfidPower" label="功率:">
                    <el-input-number v-model="rfidEquipment.rfidPower"  style="width: 100%;" :min="1" :max="30" label="描述文字"></el-input-number>
                </el-form-item>
            </el-form>
            <div style="text-align: center;font-size:12px;color:#000">备注：天线的输出功率范围是1~30dBm，功率越大识别距离越远</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="rfidShowDialog = false">取 消</el-button>
                <el-button type="primary" @click="confirmRfidCardNo">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>


<script>
//RFID设备管理
import AddRfidEquipment from './add.vue'
import PhotoView from './photo.vue'
import { tableOption } from '@/const/crud/business/rfid-equipment'
import * as API from '@/api/business/rfid-equipment'
export default {
    components: { AddRfidEquipment, PhotoView },
    data() {
        return {
            tableData: [],
            tableOption: tableOption,
            tableLoading: false,
            rfidShowDialog:false,
            tablePage: {
                size: 50,
                current: 1,
                total: 0
            },
            rfidEquipment: {
                rfidEquipmentId: '',
                rfidEquipmentName: '',
                rfidEquipmentCode: '',
                orgId: this.$store.state.data.globalOrg.id,
                rfidPower:''
            }
        }
    },
    methods: {
        addRfidEquipment() {
            this.$refs['add-rfid-equipment'].show()
        },
        // seePhoto(row){
        //     this.$refs['photo-view'].show(row.photoId)
        // },
        query() {
            this.tableLoading = true
            API.page(Object.assign(this.tablePage,this.rfidEquipment)).then(res => {
                this.tableLoading = false
                if (res.data.code === 0) {
                    var result = res.data.data.records
                    result.map((item) => {
                        item.createTime = this.dateFormat(item.createTime)
                        item.rfidPower = item.rfidPower//+"dBp"
                        item.isAlarmName = item.isAlarm==0?'否':'是'
                    })
                    this.tableData = result
                    this.tablePage.total = Number.parseInt(res.data.data.total)
                }
            })
        },
        handleEdit(row, index) {
            this.$refs['add-rfid-equipment'].edit(row)
        },
        rowDel(row, index) {
            this.$confirm('是否删除当前数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                API.deleteById(row.rfidEquipmentId).then(res => {
                    if(res.data.code === 0){
                        this.$message.success('删除成功')
                        this.query()
                    }
                })
            })
        },
        dateFormat(data){
            var dat = new Date(data).toJSON();
            var rDate = new Date(+new Date(dat)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'')
            return rDate;
        },
        invData(row){
            console.log(row)

          let cmd  ={}
          if(row.isInv === '1') {
            cmd = {clientId: row.equipmentId, command: "inventory", 'ip': row.rfidIp}
          }else{
            cmd={clientId:row.equipmentId,command:'stop',ip:row.rfidIp}
          }
          API.command(cmd).then(res => {
            if (res.data.code === 0) {
              this.query()
            }
          })

        },
        connect(row){
          let cmd  ={}
          if(row.isConnect === '1') {
             cmd = {clientId: row.equipmentId, command: "connect", 'ip': row.rfidIp}
          }else{
            cmd={clientId:row.equipmentId,command:'disconnect',ip:row.rfidIp}
          }
            API.command(cmd).then(res => {
              if (res.data.code === 0) {
                this.query()
              }
            })

        },
        setPower(row){
            this.rfidShowDialog = true
            this.rfidEquipment.rfidEquipmentId = row.rfidEquipmentId
            this.rfidEquipment.rfidPower = row.rfidPower
        },
        confirmRfidCardNo(){
            this.$refs['rfidInfo'].validate(valid=>{
                if(valid&&this.rfidEquipment.rfidEquipmentId){
                    API.bindRfidPower(this.rfidEquipment).then(res=>{
                        if(res.data.code === 0){
                            this.$message.success('功率设置成功')
                            this.handleClose()
                            this.query()
                        }
                    })
                }
            })
        },
        handleClose(){
            this.rfidShowDialog = false
        }
    },
    mounted() {
        this.query()
    }
}
</script>
